package com.beiye.drivertransport.chart;

import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import java.util.List;

/**
 * 作者：chs on 2016/9/8 16:25
 * 邮箱：657083984@qq.com
 * 饼状图表
 */
//单独处理三个数据
public class NewPieChart1 extends View {
    /**
     * 视图的宽和高
     */
    private int mTotalWidth, mTotalHeight;
    /**
     * 绘制区域的半径
     */
    private float mRadius;

    private Paint mPaint,mLinePaint,mTextPaint;

    private Path mPath;
    /**
     * 扇形的绘制区域
     */
    private RectF mRectF;
    /**
     * 点击之后的扇形的绘制区域
     */
    private RectF mRectFTouch;

    private List<PieDataEntity> mDataList;
    /**
     * 所有的数据加起来的总值
     */
    private float mTotalValue;
    /**
     * 起始角度的集合
     */
    private float[] angles;
    /**
     * 手点击的部分的position
     */
    private int position = -1;
    /**
     * 点击监听
     */
    private OnItemPieClickListener mOnItemPieClickListener;

    public void setOnItemPieClickListener(OnItemPieClickListener onItemPieClickListener) {
        mOnItemPieClickListener = onItemPieClickListener;
    }

    public interface OnItemPieClickListener {
        void onClick(int position);
    }
    public NewPieChart1(Context context) {
        super(context);
        init(context);
    }

    public NewPieChart1(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public NewPieChart1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mRectF = new RectF();
        mRectFTouch = new RectF();

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);

        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStyle(Paint.Style.FILL);
        mLinePaint.setStrokeWidth(2);
        mLinePaint.setColor(Color.BLACK);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
//        int size=(int)this.getResources().getDimension(R.dimen.ourTextSize);
//        mTextPaint.setTextSize(size);

        mPath = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mTotalWidth = w - getPaddingLeft() - getPaddingRight();
        mTotalHeight = h - getPaddingTop() - getPaddingBottom();

        mRadius = (float) (Math.min(mTotalWidth,mTotalHeight)/2*1);

        mRectF.left = -mRadius;
        mRectF.top = -mRadius;
        mRectF.right = mRadius;
        mRectF.bottom = mRadius;

        mRectFTouch.left = -mRadius-6;
        mRectFTouch.top = -mRadius-6;
        mRectFTouch.right = mRadius+6;
        mRectFTouch.bottom = mRadius+6;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mDataList==null)
            return;
        canvas.translate(mTotalWidth/2,mTotalHeight/2);
        //绘制饼图的每块区域
        drawPiePath(canvas);
    }
    private float percent = 0f;
    private TimeInterpolator pointInterpolator = new DecelerateInterpolator();
    public void startAnimation(int duration){
        ValueAnimator mAnimator = ValueAnimator.ofFloat(0,1);
        mAnimator.setDuration(duration);
        mAnimator.setInterpolator(pointInterpolator);
        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                percent = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        mAnimator.start();
    }
    /**
     * 绘制饼图的每块区域 和文本
     * @param canvas
     */
    private void drawPiePath(Canvas canvas) {
        //起始地角度
        float startAngle = 30;
        for(int i = 0;i<mDataList.size();i++){
            float sweepAngle = mDataList.get(i).getValue()/mTotalValue*360-1;//每个扇形的角度
            sweepAngle = sweepAngle * percent;

            mPaint.setColor(mDataList.get(i).getColor());
            if(position-1==i){
                canvas.drawArc(mRectFTouch,startAngle,sweepAngle,true,mPaint);
            }else {
                canvas.drawArc(mRectF,startAngle,sweepAngle,true,mPaint);
            }


            //确定直线的起始和结束的点的位置
            float pxs = (float) (mRadius* Math.cos(Math.toRadians(startAngle+sweepAngle/2)));
            float pys = (float) (mRadius* Math.sin(Math.toRadians(startAngle+sweepAngle/2)));
            float pxt = (float) (mRadius* Math.cos(Math.toRadians(startAngle+sweepAngle/2)));
            float pyt = (float) (mRadius* Math.sin(Math.toRadians(startAngle+sweepAngle/2)));
            angles[i] = startAngle;

            startAngle += sweepAngle+1;
            //绘制线和文本
            canvas.drawLine(pxs,pys,pxt,pyt,mLinePaint);
        }


        mPaint.setColor(Color.GRAY);
        mPaint.setTextSize(20);
        //起始地角度
        float startAngle1 = 30;
        for(int i=0;i<mDataList.size();i++){
            float sweepAngle = mDataList.get(i).getValue()/mTotalValue*360-1;//每个扇形的角度

            //确定直线的起始和结束的点的位置
            float pxs = (float) (mRadius* Math.cos(Math.toRadians(startAngle1+sweepAngle/2)));
            float pys = (float) (mRadius* Math.sin(Math.toRadians(startAngle1+sweepAngle/2)));
            if(i==0){
                canvas.drawLine(pxs/2,pys/3*2,mRadius+20,mRadius/4,mPaint);
                canvas.drawLine(mRadius+20,mRadius/4,mRadius*3,mRadius/4,mPaint);
                if(judgeLength(mDataList.get(0).getName())){
                    canvas.drawText(mDataList.get(0).getName(),mRadius+20,mRadius/4-10,mPaint);
                }else{
                    canvas.drawText(mDataList.get(0).getName().substring(0,6),mRadius+20,mRadius/4-40,mPaint);
                    canvas.drawText(mDataList.get(0).getName().substring(6),mRadius+20,mRadius/4-10,mPaint);
                }
            }else if(i==1){
                canvas.drawLine(pxs/2,pys/3*2,-mRadius-20,-mRadius/3,mPaint);
                canvas.drawLine(-mRadius-20,-mRadius/3,-mRadius*2,-mRadius/3,mPaint);
                if(judgeLength(mDataList.get(1).getName())){
                    canvas.drawText(mDataList.get(1).getName(),-mRadius*2,-mRadius/3-10,mPaint);
                }else{
                    canvas.drawText(mDataList.get(1).getName().substring(0,6),-mRadius*2,-mRadius/3-40,mPaint);
                    canvas.drawText(mDataList.get(1).getName().substring(6),-mRadius*2,-mRadius/3-10,mPaint);
                }
            }else if(i==2){
                canvas.drawLine(pxs/2,pys/3*2,mRadius+20,-mRadius/2,mPaint);
                canvas.drawLine(mRadius+20,-mRadius/2,mRadius*3,-mRadius/2,mPaint);
                if(judgeLength(mDataList.get(2).getName())){
                    canvas.drawText(mDataList.get(2).getName(),mRadius+20,-mRadius/2-10,mPaint);
                }else{
                    canvas.drawText(mDataList.get(2).getName().substring(0,6),mRadius+20,-mRadius/2-40,mPaint);
                    canvas.drawText(mDataList.get(2).getName().substring(6),mRadius+20,-mRadius/2-10,mPaint);
                }
            }
            startAngle1 += sweepAngle+1;
        }
        /*//canvas.drawLine(mRadius/2,mRadius/2,mRadius+20,mRadius/4,mPaint);
        canvas.drawLine(mRadius+20,mRadius/4,mRadius*3,mRadius/4,mPaint);
        if(judgeLength(mDataList.get(0).getName())){
            canvas.drawText(mDataList.get(0).getName(),mRadius+20,mRadius/4-10,mPaint);
        }else{
            canvas.drawText(mDataList.get(0).getName().substring(0,6),mRadius+20,mRadius/4-40,mPaint);
            canvas.drawText(mDataList.get(0).getName().substring(6),mRadius+20,mRadius/4-10,mPaint);
        }

        //canvas.drawLine(-mRadius/2,-40,-mRadius-20,-mRadius/2,mPaint);
        canvas.drawLine(-mRadius-20,-2*mRadius/3,-mRadius*2,-2*mRadius/3,mPaint);
        if(judgeLength(mDataList.get(1).getName())){
            canvas.drawText(mDataList.get(1).getName(),-mRadius*2,-2*mRadius/3-10,mPaint);
        }else{
            canvas.drawText(mDataList.get(1).getName().substring(0,6),-mRadius*2,-2*mRadius/3-40,mPaint);
            canvas.drawText(mDataList.get(1).getName().substring(6),-mRadius*2,-2*mRadius/3-10,mPaint);
        }

        if(mDataList.size()>2){
            //canvas.drawLine(mRadius/2,20,mRadius+20,-mRadius/2,mPaint);
            canvas.drawLine(mRadius+20,-mRadius/2,mRadius*3,-mRadius/2,mPaint);
            if(judgeLength(mDataList.get(2).getName())){
                canvas.drawText(mDataList.get(2).getName(),mRadius+20,-mRadius/2-10,mPaint);
            }else{
                canvas.drawText(mDataList.get(2).getName().substring(0,6),mRadius+20,-mRadius/2-40,mPaint);
                canvas.drawText(mDataList.get(2).getName().substring(6),mRadius+20,-mRadius/2-10,mPaint);
            }
        }*/

    }

    public void setDataList(List<PieDataEntity> dataList){
        this.mDataList = dataList;
        mTotalValue = 0;
        for(PieDataEntity pieData :mDataList){
            mTotalValue +=pieData.getValue();
        }
        angles = new float[mDataList.size()];
        invalidate();
    }

    public boolean judgeLength(String str){
        if(str.split("：")[0].length()<7){
            return true;
        }else{
            return false;
        }
    }
}
